<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/treetable/jquery.treetable.css"/>
    <link rel="stylesheet" href="/css/treetable/jquery.treetable.theme.default.css"/>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
    <script src="/js/libs/jquery-2.1.1.min.js"></script>
    <script src="/js/bootstrap/bootstrap.min.js"></script>
    <script src="/js/bootstrap/bootstrap-select.min.js"></script>
    <script src="/js/jq.js"></script>
    <script src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
    <script src="/js/common.js"></script>
    <script src="/layui/layui.js"></script>
    <script src="/js/dict.js"></script>
    <script src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
    <script src="/js/my/ztree-menu.js"></script>
    <script src="/js/my/permission.js"></script>
    <script src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
    <script src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
    <script src="/js/libs/jquery.treetable.js"></script>
</head>
<body>
<div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <div>
            <header style="height: 100%">
                <div align="left">
                    <table style="width: 100%">
                        <tr>
                            <td>
                                <form class="form-inline" onsubmit="return false">
                                    <div class="form-group col-md-12">
                                        <label>编号：</label>
                                        <input id="name" type="text" class="form-control" placeholder="编号">
                                        &nbsp; &nbsp;
                                        <label> 部门：</label>
                                        <input class='form-control' type='hidden' name='originatorOfficeId'
                                               id='originatorOfficeId'>
                                        <input style="width: 10%;" class='form-control' placeholder='部门' type='text'
                                               name='originatorOfficeName' id='originatorOfficeName' readonly>
                                        <a href="#" style="width: 5%; height: 32px; line-height: 32px;"
                                           class="layui-btn layui-btn-sm"
                                           onclick="showSpecialTree('originatorOfficeId', 'originatorOfficeName', 'dept', '部门', 'root', 'select','fasDeptTree')">
                                            <i class="layui-icon">&#xe615;</i>
                                        </a>
                                        &nbsp; &nbsp;
                                        <label>公司：</label>
                                        <select class="form-control input-sm col-md-1 selectpicker show-tick"
                                                name="companyId" id="companyId"
                                                title="公司" data-live-search="true">
                                        </select>
                                        &nbsp; &nbsp;
                                        <label>创建时间：</label>
                                        <input type="text" class="form-control" placeholder="开始时间" id="beginTime">
                                        <input type="text" class="form-control" placeholder="结束时间" id="endTime">
                                        <br><br>
                                        <label>商品名称： </label>
                                        <input id="goodsName" name="goodsName" type="text" class="form-control"
                                               placeholder="商品名称">
                                    </div>
                                </form>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <button id="searchBt" class="layui-btn layui-btn-sm search"><i class="layui-icon">&#xe615;</i>搜索
                                </button>
                            </td>

                        </tr>
                    </table>
                </div>
            </header>

            <div>
                <div class="widget-body no-padding">
                    <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>主单编号</th>
                            <th>需求人</th>
                            <th>需求部门</th>
                            <th>公司名称</th>
                            <th>商品</th>
                            <th>规格</th>
                            <th>单位</th>
                            <th>交付状态</th>
                            <th>类型</th>
                            <th>需求数量</th>
                            <th>已交付数量</th>
                            <th>待交付数量</th>
                            <th>单价</th>
                            <th>总价</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

<script type="text/javascript">
    var pers = checkPermission();
    layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#beginTime'
        });
        laydate.render({
            elem: '#endTime'
        });
    });
    $(function () {
        showDictSelect('companyId', 'company', '请选择');
    });
    var example;

    function init() {
        example =
            $('#dt-table').DataTable({
                "searching": false,
                "processing": false,
                "serverSide": true,
                "language": {
                    "url": "/js/plugin/datatables/Chinese.lang"
                },
                "ajax": {
                    "url": "/orderDetail/list",
                    "type": "get",
                    "data": function (d) {
                        d.name = $("#name").val().trim();
                        d.originatorOfficeId = $('#originatorOfficeId').val().trim();
                        d.orderDetailType = 2;
                        d.companyId = $('#companyId').val().trim();
                        d.beginTime = $("#beginTime").val();
                        d.endTime = $("#endTime").val();
                        d.goodsName = $("#goodsName").val();
                    }
                },
                "dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",
                "columns": [
                    {"data": "name", "defaultContent": "", "orderable": true},
                    {"data": "orderName", "defaultContent": "", "orderable": false},
                    {"data": "originatorName", "defaultContent": "", "orderable": false},
                    {"data": "originatorOfficeName", "defaultContent": "", "orderable": false},
                    {"data": "companyName1", "defaultContent": "", "orderable": false},
                    {"data": "goodsName", "defaultContent": "", "orderable": false},
                    // {"data" : "categoryName", "defaultContent" : ""},
                    // {
                    //     "data": "isCommon",
                    //     "defaultContent": "",aria-label
                    //     "render": function (data, type, row) {
                    //         return getDict('boolean')[data];
                    //     }
                    // },
                    // {
                    //     "data": "isConsumables",
                    //     "defaultContent": "",
                    //     "render": function (data, type, row) {
                    //         return getDict('boolean')[data];
                    //     }
                    // },
                    {"data": "detailSpec", "defaultContent": "", "orderable": false},
                    {"data": "detailUnit", "defaultContent": "", "orderable": false},
                    {
                        "data": "orderDetailType",
                        "defaultContent": "",
                        "orderable": false,
                        "render": function (data, type, row) {
                            return getDict('detail_status')[data];
                        }
                    },
                    {
                        "data": "demandType",
                        "defaultContent": "",
                        "orderable": false,
                        "render": function (data, type, row) {
                            return getDict('sm_handleType')[data];
                        }
                    },
                    {"data": "detailNum", "defaultContent": "", "orderable": false},
                    {"data": "actNum", "defaultContent": "", "orderable": false},
                    {"data": "waitNum", "defaultContent": "", "orderable": false},
                    {"data": "detailSinprice", "defaultContent": "", "orderable": false},
                    {"data": "detailTotalPrice", "defaultContent": "", "orderable": false},
                    {
                        "data": "",
                        "defaultContent": "",
                        "orderable": false,
                        "render": function (data, type, row) {
                            var orderDetailType = row['orderDetailType'];
                            var id = row['id'];
                            var href = "updateOrderDetail.html?id=" + id;
                            var deliverBtn = buttonOther(row, "delivered", "交付", "fas:order:deliver", pers);
                            var cancelBtn = buttonOther(row, "cancel", "取消交付", "fas:order:cancel", pers);
                            return deliverBtn + cancelBtn;
                        }
                    },

                ],
                "order": [[0, "asc"]]
            });
    }

    function approval(id, pId, pName, num, isCommon, type) {
        //var uri="/orderDetail/approval";
        //waitNum待交付数量 deliverDiv 弹出层
        if ("delivered" == type) {
            layer.open({
                id: "submit",
                type: 2,
                title: '交付',
                // skin: "layui-layer-molv",
                shadeClose: true,
                shade: 0.3,
                maxmin: true, //开启最大化最小化按钮
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    body.contents().find("#waitNum").val(num);
                    body.contents().find("#quantity").val(num);
                    body.contents().find("#delivererId").val(pId);
                    body.contents().find("#delivererName").val(pName);
                    body.contents().find("#orderDetailId").val(id);
                },
                area: ['800px', '500px'],
                content: ['submit.html', 'yes'],
                btn: ["关闭", "交付", "交付并平单"],
                yes: function (index, layero) {
                    layer.close(index);
                },
                btn2: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    var data = body.contents().find("#form").serializeObject();
                    data.goodsType = isCommon;
                    data.type = 'delivered';
                    return submit(data, 'deliver');
                },
                btn3: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    var data = body.contents().find("#form").serializeObject();
                    data.goodsType = isCommon;
                    data.type = 'forceFinish';
                    submit(data, 'forceFinish');
                }
            });
        } else if ("cancel" == type) {
            layer.confirm('确定要取消交付吗？', {
                btn: ['确定', '取消']
            }, function () {
                $.ajax({
                    type: 'put',
                    url: '/orderDetail/cancel/' + id,
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        layer.msg(data.message, {shift: -1, time: 1000}, function () {
                            if (data.code === 0) {
                                example.ajax.reload();
                            }
                        });
                    }
                });
                layer.close(1);
            });
        }
    }

    //交付或交付并平单
    function submit(data, type) {
        var delivererId = data.delivererId;
        var delivererName = data.delivererName;
        var goodsType = data.goodsType;
        var quantity = parseInt(data.quantity, 10);
        var waitNum = parseInt(data.waitNum, 10);
        if ('forceFinish' == type) {//交付平单
            if (isNaN(quantity)) {
                layer.msg('交付数量不能为空');
                return false;
            } else if (quantity > waitNum) {
                layer.msg('交付数量不能大于待交付数量');
                return false;
            } else if (quantity > 0) {
                if (goodsType == 0 && (delivererName == '' || delivererId == '')) {
                    layer.msg('非常规性商品必须选择交付人');
                    return false;
                } else {
                    debugger;
                    $.ajax({
                        type: 'post',
                        url: '/orderDetail/deliver',
                        data: data,
                        //contentType: "application/json; charset=utf-8",
                        success: function (data) {
                            layer.msg(data.message, {shift: -1, time: 1000}, function () {
                                if (data.code === 0) {
                                    example.ajax.reload();
                                }
                            });
                        }
                    });
                }
            } else {
                $.ajax({
                    type: 'post',
                    url: '/orderDetail/deliver',
                    data: data,
                    //contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        layer.msg(data.message, {shift: -1, time: 1000}, function () {
                            if (data.code === 0) {
                                example.ajax.reload();
                            }
                        });
                    }
                });
            }
        } else {//交付
            if (quantity == 0 || isNaN(quantity)) {
                layer.msg('交付数量不能为空');
                return false;
            } else if (quantity > waitNum) {
                layer.msg('交付数量不能大于待交付数量');
                return false;
            } else if (goodsType == 0 && (delivererName == '' || delivererId == '')) {
                layer.msg('非常规性商品必须选择交付人');
                return false;
            } else {
                debugger;
                $.ajax({
                    type: 'post',
                    url: '/orderDetail/deliver',
                    data: data,
                    // contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        layer.msg(data.message, {shift: -1, time: 1000}, function () {
                            if (data.code === 0) {
                                example.ajax.reload();
                            }
                        });
                    }
                });
            }
        }
    }

    //生成交付和取消交付按钮的方法
    function buttonOther(obj, type, title, permission, pers) {
        if (permission != "") {
            if ($.inArray(permission, pers) < 0) {
                return "";
            }
        }
        // var btn = $("<button class='layui-btn layui-btn-xs' title='编辑' onclick='window.location=\"" + href +"\"'><i class='layui-icon'>&#xe642;</i></button>");
        var btn = $("<button class='layui-btn layui-btn-xs' title='" + title + "' onclick=\"approval('" + obj['id'] + "','" + obj['originatorId'] + "','" + obj['originatorName'] + "','" + obj['waitNum'] + "','" + obj['isCommon'] + "','" + type + "')\">" + title + "</button>");

        return btn.prop("outerHTML");
    }

    layui.use('layer', function () {
        var layer = layui.layer;
    });

    function del(id) {
        layer.confirm('确定要删除吗？', {
            btn: ['确定', '取消']
        }, function () {
            $.ajax({
                type: 'delete',
                url: '/orderDetail/' + id,
                success: function (data) {
                    example.ajax.reload();
                    layer.msg("删除成功");
                }
            });

            layer.close(1);
        });
    }

    $("#searchBt").click(function () {
        example.ajax.reload();
    });

    init();
</script>
